<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>智慧社区后台</title>
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/AdminLTE.min.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/bootstrap-switch/css/bootstrap-switch.min.css">
</head>
<style>
</style>
<body id="body" style="padding-bottom: 90px; background-color: #ECF0F5;">
	<div class="modal-content">
		<div class="modal-body">
			<div class="input-group">
				<span class="input-group-addon">标题：</span> <input type="text" class="form-control" id="title">
			</div>
			<br />
			<div class="input-group">
				<span class="input-group-addon">活动类型：</span> <select class="form-control" id="typeId">

				</select>
			</div>
			<br />
			<div class="input-group">
				<span class="input-group-addon">发布人：</span> <input type="text" class="form-control" id="publisher">
			</div>
			<br />
			<div class="input-group">
				<span class="input-group-addon">联系人：</span> <input type="text" class="form-control" id="contacts">
			</div>
			<br />
			<div class="input-group">
				<span class="input-group-addon">联系电话：</span> <input type="text" class="form-control" id="tel">
			</div>
			<br />
			<div class="input-group">
				<span class="input-group-addon">活动地址：</span> <input type="text" class="form-control" id="addr">
			</div>
			<br />
			<div class="input-group">
				<span class="input-group-addon">报名人数：</span> <input type="text" class="form-control" id="signNum" oninput="value=value.replace(/[^\d]/g,'')">
			</div>
			<br />
			<div class="input-group">
				<span class="input-group-addon">时间：</span> <input type="text" class="form-control" id="time">
			</div>
			<br />
			<div class="form-group">
				<label for="mien_pic" class="control-label">活动图片(238*178)：</label>
				<button id="imgBtn0" class="btn btn-primary glyphicon glyphicon-plus" onclick="imgFile0.click();">上传</button>
				<img id="image0" class="img-rounded" src="" width="150px;" style="cursor: pointer;" onclick="imgFile0.click();" /> <input type="file" class="form-control" style="display: none;" name="imgFile" id="imgFile0" onchange="fileChange()">
			</div>
			<div class="form-group">
				<label for="mien_pic" class="control-label">活动内容：</label>
				<textarea class="form-control" style="width: 100%; height: 500px;" id="column1_textarea" name="kindEditor_content"></textarea>
			</div>
		</div>
	</div>
</body>
<script src="${pageContext.request.contextPath}/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-switch/js/bootstrap-switch.min.js"></script>
<!-- layer -->
<script src="${pageContext.request.contextPath}/plugins/layer/2.4/layer.js"></script>
<script src="${pageContext.request.contextPath}/plugins/laydate/laydate.js"></script>
<script src="${pageContext.request.contextPath}/js/ajaxfileupload.js"></script>
<script src="${pageContext.request.contextPath}/js/date.js"></script>

<!-- kindeditor 编辑器 -->
<script charset="utf-8" src="${pageContext.request.contextPath}/kindeditor-4.1.10/kindeditor.js"></script>
<script charset="utf-8" src="${pageContext.request.contextPath}/kindeditor-4.1.10/lang/zh_CN.js"></script>
<script>
	var lock = false;
	var id = "${map['id']}";
	var index = parent.layer.getFrameIndex(window.name);
	var imgUrl;
	var editor;
	var options = {
		items : [ 'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'code', 'cut', 'copy', 'paste', 'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
				'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript', 'superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', '/',
				'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image', 'multiimage',
				'flash', 'media', 'insertfile', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak', 'anchor', 'link', 'unlink', '|', 'about' ], //配置kindeditor编辑器的工具栏菜单项
		cssPath : 'kindeditor-4.1.10/plugins/code/prettify.css',
		allowFileManager : true,
		filterMode : true, //是否启用过滤模式(编辑文本框中是否可以出现控件显示)
		htmlTags : {
			span : [ '.color', '.background-color', '.font-size', '.font-family', '.background', '.font-weight', '.font-style', '.text-decoration', '.vertical-align', '.line-height' ],
			font : [ 'id', 'class', 'color', 'size', 'face', '.background-color' ],
			a : [ 'id', 'class', 'href', 'target', 'name' ],
			embed : [ 'id', 'class', 'src', 'width', 'height', 'type', 'loop', 'autostart', 'quality', '.width', '.height', 'align', 'allowscriptaccess' ],
			img : [ 'id', 'class', 'src', 'width', 'height', 'border', 'alt', 'title', 'align', '.width', '.height', '.border' ],
			'p,ol,ul,li,blockquote,h1,h2,h3,h4,h5,h6' : [ 'id', 'class', 'align', '.text-align', '.color', '.background-color', '.font-size', '.font-family', '.background', '.font-weight',
					'.font-style', '.text-decoration', '.vertical-align', '.text-indent', '.margin-left' ],
			pre : [ 'id', 'class' ],
			hr : [ 'id', 'class', '.page-break-after' ],
			'br,tbody,tr,strong,b,sub,sup,em,i,u,strike,s,del' : [ 'id', 'class' ],
			iframe : [ 'id', 'class', 'src', 'frameborder', 'width', 'height', '.width', '.height' ]
		},
		allowFileManager : true,
		allowUpload : true, //允许上传图片
		imageUploadJson : '/saveImg', //服务端上传图片处理URI
		uploadJson : '${pageContext.request.contextPath}/upload_img', //上传
		// 		fileManagerJson : '${pageContext.request.contextPath}/mien_file_manager.do', // 文件管理
		resizeType : 0,//使其不可以拖动 
		afterCreate : function() {
		},
		afterBlur : function() {
			this.sync();
		}
	}
	KindEditor.ready(function(K) {
		editor = K.create('textarea[name="kindEditor_content"]', options);
	});

	laydate.render({
		elem : '#time',
		min : new Date().toLocaleDateString(),
		range : true
	});

	$(function() {
		$.ajax({
			url : "${pageContext.request.contextPath}/selectPropertyActivityTypeByAll",
			type : "post",
			dataType : "json",
			success : function(map) {
				var list = map['list'];
				var options = "<option value=''>请选择...</option>";
				for (var i = 0; i < list.length; i++) {
					options += "<option value='"+list[i].id+"'>" + list[i].name + "</option>";
				}
				$("#typeId").html(options);
				if (id != '') {
					$.ajax({
						url : "${pageContext.request.contextPath}/selectPropertyActivityById",
						type : "post",
						data : {
							id : id
						},
						dataType : "json",
						success : function(map) {
							var activity = map['activity'];
							$("#title").val(activity.title);
							$("#publisher").val(activity.publisher);
							$("#contacts").val(activity.contacts);
							$("#tel").val(activity.tel);
							$("#typeId").val(activity.typeId);
							$("#addr").val(activity.addr);
							$("#signNum").val(activity.signNum);
							if (activity.startTime != null & activity.endTime != null) {
								$("#time").val(new Date(activity.startTime).Format("yyyy-MM-dd") + " - " + new Date(activity.endTime).Format("yyyy-MM-dd"));
							}
							imgUrl = activity.s2;
							if (imgUrl != null & imgUrl != '') {
								$('#image0').attr('src', '${pageContext.request.contextPath}/' + imgUrl);
								$('#image0').attr('style', 'width: 238px; height: 178px;');
								$("#image0").show();
								$("#imgBtn0").hide();
							}
							editor.html(activity.content);
							$("#column1_textarea").val(activity.content);
						}
					});
				}
			}
		});
	});

	//添加或修改
	function insertExplodingActivity() {
		lock = true;
		var title = $("#title").val();
		var publisher = $("#publisher").val();
		var contacts = $("#contacts").val();
		var tel = $("#tel").val();
		var addr = $("#addr").val();
		var typeId = $("#typeId").val();
		var signNum = $("#signNum").val();
		var time = $("#time").val();
		var startTime = "";
		var endTime = "";
		var column1_textarea = $("#column1_textarea").val();
		var phone = /^1\d{10}$/;
		if (title === '' || publisher === '' || tel === '' || typeId === '' || signNum === '' || contacts === '' || addr === '' || column1_textarea === '') {
			layer.alert("请完整填写数据！");
			lock = false;
			return;
		}
		if (time != null & time != '') {
			startTime = time.split("-")[0] + "-" + time.split("-")[1] + "-" + time.split("-")[2];
			endTime = time.split("-")[3] + "-" + time.split("-")[4] + "-" + time.split("-")[5];
		} else {
			layer.alert("请完整填写数据！");
			lock = false;
			return;
		}
		if (phone.test(tel) == false) {
			layer.alert("手机输入不合法!");
			that.lock = false;
			return;
		}
		$.ajax({
			url : '${pageContext.request.contextPath}/insertExplodingActivity',
			type : "post",
			data : {
				id : id,
				title : title,
				publisher : publisher,
				s2 : imgUrl,
				tel : tel,
				contacts : contacts,
				typeId : typeId,
				addr : addr,
				signNum : signNum,
				startTimeStr : startTime,
				endTimeStr : endTime,
				content : column1_textarea
			},
			dataType : "json",
			success : function(data) {
				var result = data['result'];
				if (result == 1) {
					layer.msg('操作成功!', {
						icon : 1,
						time : 800
					});
					setTimeout(function() {
						parent.query();
						parent.layer.close(index);
					}, 1000);
				} else {
					layer.msg('操作失败!', {
						icon : 2,
						time : 800
					});
					lock = false;
				}
			}
		});
	}

	function fileChange() {
		$.ajaxFileUpload({
			fileElementId : 'imgFile0', //需要上传的文件域的ID，即<input type="file">的ID。
			url : "${pageContext.request.contextPath}/upload_img", //后台方法的路径
			type : 'post', //当要提交自定义参数时，这个参数要设置成post
			dataType : 'json', //服务器返回的数据类型。可以为xml,script,json,html。如果不填写，jQuery会自动判断。
			secureuri : false, //是否启用安全提交，默认为false。
			async : true, //是否是异步
			success : function(data) { //提交成功后自动执行的处理函数，参数data就是服务器返回的数据。
				imgUrl = data.url;
				$('#image0').attr('src', '${pageContext.request.contextPath}/' + data.url);
				$('#image0').attr('style', 'width: 238px; height: 178px;');
				$("#image0").show();
				$("#imgBtn0").hide();
			},
			error : function(data, status, e) { //提交失败自动执行的处理函数。
				console.error(e);
			}
		});
	}

	function image_check(feid) { //自己添加的文件后缀名的验证
		var img = document.getElementById(feid);
		return /.(jpg|png|gif|bmp)$/.test(img.value) ? true : (function() {
			layer.alert('图片格式仅支持jpg、png、gif、bmp格式，且区分大小写。');
			return false;
		})();
	}

	function insert() {
		if (!lock) {
			insertExplodingActivity();
		}
	}
</script>
</html>
